package com.nj.dms.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nj.dms.bean.Essay;
import com.nj.dms.dao.EssayDao;
import com.nj.dms.dao.impl.EssayDaoImpl;

/**
 * 查找一个文章
 */
@WebServlet("/FindEssay")
public class FindEssay extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int id = new Integer(request.getParameter("id"));

		EssayDao essayDao = new EssayDaoImpl();

		Essay essay = essayDao.findEssay(id);

		if (essay != null) {

			// 文章已经找到，执行服务器跳转，并把请求传递下去

			request.setAttribute("essay", essay);

			request.getRequestDispatcher("document/look-essay.jsp").forward(request, response);

		} else {
			// 文章未找到， 跳转到我的文章菜单页面

			response.sendRedirect("document/my-document-menu.jsp");

		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
